package com.ttc.ssm.service.home.impl;

import com.ttc.ssm.common.MailPage;
import com.ttc.ssm.controller.common;
import com.ttc.ssm.mapper.home.HomeCompanyMapper;
import com.ttc.ssm.mapper.home.HomeDeliveryActivityMapper;
import com.ttc.ssm.mapper.home.HomeInviteActivityMapper;
import com.ttc.ssm.po.Company;
import com.ttc.ssm.po.CompanyDetail;
import com.ttc.ssm.po.DeliveryActivity;
import com.ttc.ssm.po.InviteActivity;
import com.ttc.ssm.service.home.HomeCompanyService;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * Created by Jiarui on 2016/8/5.
 */
public class HomeCompanyServiceImpl implements HomeCompanyService {

    @Autowired
    HomeCompanyMapper homeCompanyMapper;

    @Autowired
    HomeDeliveryActivityMapper homeDeliveryActivityMapper;

    @Autowired
    HomeInviteActivityMapper homeInviteActivityMapper;

    @Override
    public int register_company(Company company, HttpServletRequest request) throws Exception {
        Date now = new Date();
        company.setDate_added(now);
        company.setDate_modified(now);
        company.setStatus(1);
        company.setSalt(common.getSalt());
        String password = common.SHA1(company.getSalt()+common.SHA1(company.getSalt()+common.SHA1(company.getPassword())));
        company.setPassword(password);
        company.setIp(common.getIpAddr(request));
        homeCompanyMapper.register_company(company);
        return company.getCompany_id();
    }

    @Override
    public int save_psd(Company company) throws Exception {
        company.setDate_modified(new Date());
        String password = common.SHA1(company.getSalt()+common.SHA1(company.getSalt()+common.SHA1(company.getPassword())));
        company.setPassword(password);
        homeCompanyMapper.save_psd(company);
        return company.getCompany_id();
    }

    @Override
    public Company selectCompanyById(int company_id) throws Exception {
        return homeCompanyMapper.selectById(company_id);
    }

    @Override
    public CompanyDetail selectCompanyDetail(int company_id) throws Exception {
        return homeCompanyMapper.selectCompanyDetail(company_id);
    }

    @Override
    public int selectStatusByCompanyId(int company_id) throws Exception {
        return homeCompanyMapper.selectStatusByCompanyId(company_id);
    }

    @Override
    public int selectDetailStatusByCompanyId(int company_id) throws Exception {
        return homeCompanyMapper.selectDetailStatusByCompanyId(company_id);
    }

    @Override
    public int countRowsNoReadCompanyDelivery(int company_id) throws Exception {
        return homeDeliveryActivityMapper.countRowsNoReadCompany(company_id);
    }

    @Override
    public int countRowsNoReadCompanyDeliveryForMail(MailPage mailPage) throws Exception {
        return homeDeliveryActivityMapper.countRowsNoReadCompanyForEmail(mailPage);
    }

    @Override
    public List<DeliveryActivity> selectDeliveryActivityCompany(int company_id) throws Exception {
        return homeDeliveryActivityMapper.selectActivityCompany(company_id);
    }

    @Override
    public List<DeliveryActivity> selectDeliveryActivityCompanyForMail(MailPage mailPage) throws Exception {
        return homeDeliveryActivityMapper.selectActivityCompanyForPage(mailPage);
    }

    @Override
    public int countRowsNoReadCompany(int company_id) throws Exception {
        return homeInviteActivityMapper.countRowsNoReadCompany(company_id);
    }

    @Override
    public int countRowsNoReadCompanyForMail(MailPage mailPage) throws Exception {
        return homeInviteActivityMapper.countRowsNoReadCompanyForMail(mailPage);
    }

    @Override
    public List<InviteActivity> selectActivityCompany(int company_id) throws Exception {
        return homeInviteActivityMapper.selectActivityCompany(company_id);
    }

    @Override
    public List<InviteActivity> selectActivityCompanyForMail(MailPage mailPage) throws Exception {
        return homeInviteActivityMapper.selectActivityCompanyForPage(mailPage);
    }
}
